<?php
include('common_include.php');
include('validateSession.php');

$announcementAdd = $_POST["addAnnouncementArea"];
$operation = $_POST["operation"];
$deleteID = $_POST["deleteID"];
$announcementArr = $_POST["announcement"];
 
$dao = new SQLitePersistence();


if ($operation == 'Add' && $announcementAdd != '') {
	try {
		$dao -> saveNewAnnouncement($announcementAdd);
		header("Location: announcement_update.php");
	} catch (Exception $e) {
		header("Location: announcement_update.php?error=Save Announcement Failed.");
	}
} else if ($operation == 'Delete' && $deleteID != '') {
	try {
		$dao -> deleteAnnouncement($deleteID);
		header("Location: announcement_update.php");
	} catch (Exception $e) {
		header("Location: announcement_update.php?error=Delete Announcement Failed");
	}
} else if ($operation == 'Update' && sizeof($announcementArr) > 0) {
	try {
		for ($i=0; $i < sizeof($announcementArr); $i++) {
			/*
			echo $i;
			echo "\n";
			echo $announcementArr[$i]['announcementID'];
			echo "\n";
			echo $announcementArr[$i]['announcementDesc'];
			echo "\n";
			echo $announcementArr[$i]['announcementSeq'];
			echo "\n";
			*/
			$announcementObj = new Announcement();			
			$announcementObj->setAnnouncementId($announcementArr[$i]['announcementID']);
			$announcementObj->setAnnouncementDesc($announcementArr[$i]['announcementDesc']);
			$announcementObj->setAnnouncementSeq($announcementArr[$i]['announcementSeq']);
			$dao -> updateAnnouncementObj($announcementObj);
			unset($announcementObj);
		}
		header("Location: announcement_update.php");
	} catch (Exception $e) {
		header("Location: announcement_update.php?error=Update Announcement Failed");
	}
} else {
	Logger::logInfo("Javascript Injection disabled");
}

unset($dao);
?>